package com.zc.guoxuan.controller;

import com.alibaba.fastjson.JSONObject;
import com.zc.custom.domain.ChargeCustomOrderInfo;
import com.zc.guoxuan.domain.UserAuthentication;
import com.zc.guoxuan.util.HttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author glb
 * @Date 2023/8/3 14:11
 * @Version 1.0
 */

//@Controller
public class DistributeController {

    @Value("${moveChargeCar-platform.ip}")
    private String ip;
    @Value("${moveChargeCar-platform.port}")
    private Integer port;

    /**启动充电*/
    @PostMapping("/startCharging")
    public void  startCharging(UserAuthentication userAuthentication){
        String flowNo="C"+System.currentTimeMillis();
        String cardNo="060009700065051Z";
        Map<String, String> header=new HashMap<>();
        header.put("Content-Type","application/x-www-form-urlencoded");
        Map<String, String> body=new HashMap<>();
        body.put("flowNo",flowNo);
        body.put("pid",userAuthentication.getDeviceCode());
        body.put("gunPos",userAuthentication.getGunPos());
        body.put("cardNo",cardNo);
        body.put("sumMoney",userAuthentication.getSumMoney());
        body.put("chargeModel",userAuthentication.getChargeModel());
        if("2".equals(userAuthentication.getChargeModel())){
            body.put("timeLong",userAuthentication.getChargeValue());
        }else  if ("3".equals(userAuthentication.getChargeModel())){
            body.put("electricity",userAuthentication.getChargeValue());
        }else  if ("5".equals(userAuthentication.getChargeModel())){
            body.put("chargePrice",userAuthentication.getChargeValue());
        }
        String respond = HttpUtils.sendPost("http://"+ip+":"+port+"/pile/userAuthentication", header, body);
        System.out.println("响应："+respond);
        JSONObject jsonObject=JSONObject.parseObject(respond);
        String code = String.valueOf(jsonObject.get("code"));
        String msg = String.valueOf(jsonObject.get("msg"));
        ChargeCustomOrderInfo chargeCustomOrderInfo=new ChargeCustomOrderInfo();
        chargeCustomOrderInfo.setOrderInfoCode(flowNo);
        chargeCustomOrderInfo.setDeviceCode(userAuthentication.getDeviceCode());
        chargeCustomOrderInfo.setGunPos(Integer.parseInt(userAuthentication.getGunPos()));
        chargeCustomOrderInfo.setChargeModel(Integer.parseInt(userAuthentication.getChargeModel()));
        chargeCustomOrderInfo.setChargeQuantity(userAuthentication.getChargeValue());

        chargeCustomOrderInfo.setCustomCode(userAuthentication.getCustomCode());
        chargeCustomOrderInfo.setPaymentAmount(userAuthentication.getPaymentAmount());
        chargeCustomOrderInfo.setPaymentMethod(userAuthentication.getPaymentMethod());



    }

    /**停止充电*/
    @PostMapping("/endCharging")
    public void  endCharging(@RequestParam("deviceCode") String deviceCode, @RequestParam("gunPos") String gunPos){
        Map<String, String> header=new HashMap<>();
        header.put("Content-Type","application/x-www-form-urlencoded");
        Map<String, String> body=new HashMap<>();
        body.put("pid",deviceCode);
        body.put("moduleNos",gunPos);
        body.put("type","2");
        body.put("moduleParams","2");
        String respond = HttpUtils.sendPost("http://"+ip+":"+port+"/pile/controllerSetting", header, body);
        System.out.println("响应："+respond);
        JSONObject jsonObject=JSONObject.parseObject(respond);
        String code = String.valueOf(jsonObject.get("code"));
        String msg = String.valueOf(jsonObject.get("msg"));
    }


}
